/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import tnviet.proj.jna.utilities.AnimatingSplitPane;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import jpcap.JpcapCaptor;
import jpcap.packet.Packet;
import org.jdesktop.swingx.JXStatusBar;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.skin.SkinInfo;
import pcap.reconst.FileDataReconstructor;
import pcap.reconst.HttpDecoder;
import pcap.reconst.beans.TcpConnection;
import pcap.reconst.output.Http;
import pcap.reconst.output.HttpDecodedOutput;
import pcap.reconst.output.HttpOutput;
import pcap.reconst.reconstructor.JpcapReconstructor;
import pcap.reconst.reconstructor.PacketReassembler;
import pcap.reconst.reconstructor.TcpReassembler;
import tnviet.proj.jna.analyzer.JNPacketAnalyzer;
import tnviet.proj.jna.dialog.ColorRulesDialog;
import tnviet.proj.jna.dialog.ConfigDialog;
import tnviet.proj.jna.hexarea.JNHexPane;
import tnviet.proj.jna.stat.JNStatisticsController;
import tnviet.proj.jna.stat.JNStatisticsControllerLoader;
import tnviet.proj.jna.table.JNTablePane;
import tnviet.proj.jna.tree.JNTreePane;
import tnviet.proj.jna.utilities.JNetAnalyzerLogo;
import tnviet.proj.jna.utilities.PcapFileFilter;

/**
 *
 * @author Administrator
 */
public class JNMainFrame extends JFrame implements ActionListener, ListSelectionListener, ItemListener{

    private static final Border EMPTY_BORDER = new EmptyBorder(0, 0, 0, 0);
    private static final Border PANEL_BORDER = new EmptyBorder(10, 10, 10, 10);
    public static JFileChooser chooser;

    JNCaptor captor;
    String lookAndFeel;
    JNPacketAnalyzer[] analyzers;


    // GUI Components
    JPanel contentPane;
    ButtonGroup lookAndFeelRadioGroup;
    //JNMainPane mainPane;
    JXStatusBar statusBar;
    JLabel statusLabel;
    AnimatingSplitPane splitPane1, splitPane2;
    tnviet.proj.jna.utilities.ResourceManager resourceManager;
    JMenuBar menuBar;
    JMenu fileMenu,goMenu,viewMenu,captureMenu,statsMenu,bandwidthMenu,analysisMenu, lookAndFeelMenu,
            coreLookandfeelMenu, substanceLookAndFeelMenu, cumulativeMenu,continuousMenu,helpMenu;
    JMenuItem openMenu,closeMenu,saveMenu, saveAsMenu,configurationMenu,quitMenu,
            previousMenu, nextMenu,backMenu,forwardMenu,goToPacketMenu,firstMenu,lastMenu,
            colorRulesMenu,managementMenu,reportMenu,
            startMenu,stopMenu,restartMenu,
            blockURLMenu, blockIPMenu, blockPortMenu,  helpContentsMenu,aboutMenu,
            networkVisualizingMenu,packetsVisualizingMenu;
    JCheckBoxMenuItem mainToolbarMenu,filterToolbarMenu,statusBarMenu,packetsListMenu,
            packetDetailsMenu,packetBytesMenu, captureFilterMenu;
    JButton openButton, closeButton, saveButton, saveAsButton, configurationButton, previousButton, nextButton, backButton,
            forwardButton, goToPacketButton, startButton, stopButton, restartButton, reloadButton,colorRulesButton, helpContentsButton,
            cumulativeButton, continuousButton, quitButton, reconstructTcpButton;
    JToggleButton colorizePacketsListButton, autoSrollPacketsListButton;

    // TableView Menu
    JMenu[] tableViewMenu = new JMenu[4];
    JMenu[] popupTableViewMenu = new JMenu[4];
    JMenu itemTableView, itemPopupTableView;
    JCheckBox subItemTableView, subItemPopupTableView;
    ArrayList subItemTableViewReference, subItemPopupTableViewReference;
    JPopupMenu popup, cumulativeStatPopup, continousStatPopup;
    JToolBar toolbar;
    JNTablePane tablePane;
    JNHexPane hexPane;
    JNTreePane treePane;
    boolean autoScroll = true;
    StatisticListener statisticListener;
    //JSplitPane splitPane1, splitPane2;

    


    // Logic Components
    

    public JNMainFrame() {
        this.captor = JNCaptor.getInstance();
        tablePane = new JNTablePane(this);
        treePane = new JNTreePane();
        hexPane = new JNHexPane();
        captor.setJNetMainFrame(this);
        statisticListener = new StatisticListener();
        JNPacketAnalyzerLoader.loadDefaultAnalyzer();
        this.resourceManager = new tnviet.proj.jna.utilities.ResourceManager(this.getClass(), Program.locale);
        this.createMenuBar();
        this.setJMenuBar(menuBar);
        this.createToolbar();
        this.createStatusBar();
        this.getContentPane().add(toolbar, BorderLayout.PAGE_START);
        tablePane.setPopupTableHeader(popup);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int ret = captor.saveIfNot();
                if(ret != JOptionPane.CANCEL_OPTION){
                    setVisible(false);
                    dispose();
                    saveProperty();
                    super.windowClosed(e);
                    captor.clear();
                    Program.isCaptureFrameActived = false;
                }
            }

            @Override
            public void windowIconified(WindowEvent e) {
                super.windowIconified(e);
                if(Boolean.valueOf(Program.JNProperty.getProperty("config.miniToTray")) == Boolean.TRUE){
                    setVisible(false);
                }
            }


            

        });
        this.addPropertyChangeListener("lookAndFeel", new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                Program.JNProperty.setProperty("config.lookAndFeel", lookAndFeel);
            }
        });
        createMainPane(2);
        setStopEnabled(false);
        setRestartEnabled(false);
        setSaveAsEnabled(false);
        setSaveEnabled(false);
        setCloseEnabled(false);
        this.setIconImage(resourceManager.createImageIcon("network_disconected24.png", "Program Icon").getImage());
        this.setTitle(resourceManager.getString("Application.title"));
        this.setLocationRelativeTo(null);
        File f = new File(Program.JNProperty.getProperty("lastDir"));
        if(f.exists()){
            chooser = new JFileChooser(f);
        } else {
            chooser = new JFileChooser(new File("."));
        }
        installOtherButtonHandler();
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setFileHidingEnabled(false);
        chooser.addChoosableFileFilter(new PcapFileFilter(resourceManager.getString("pcapFilter.description"), "pcap"));
        
    }
    void clear() {
        tablePane.clear();
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if(cmd.equals("Start")){
            captor.saveIfNot();
            captor.CapturePacketsFromDevice();
            if(captor.isLiveCapture == true){
                setStopEnabled(true);
                setRestartEnabled(true);
                setStartEnabled(false);
                setOpenEnabled(false);
                setSaveEnabled(false);
                setSaveAsEnabled(false);
                setCloseEnabled(false);
                setReloadEnabled(false);
            }
        } else if(cmd.equals("Stop")){
            captor.StopCaptureFromDevice();
            setStopEnabled(false);
            setStartEnabled(true);
            setRestartEnabled(false);
            setOpenEnabled(true);
            setSaveEnabled(true);
            setSaveAsEnabled(true);
            setCloseEnabled(true);
            setReloadEnabled(true);
        } else if(cmd.equals("Restart")){
            captor.RestartCaptureFromDevice();
            setStopEnabled(true);
            setRestartEnabled(true);
            setStartEnabled(false);
            setOpenEnabled(false);
            setSaveEnabled(false);
            setSaveAsEnabled(false);
            setCloseEnabled(false);
            setReloadEnabled(false);
        } else if (cmd.equals("Next")){
            tablePane.goToNextRow();
        } else if (cmd.equals("Previous")){
            tablePane.goToPreviousRow();
        } else if(cmd.equals("Back")){
            
        } else if(cmd.equals("Forward")){
            
        } else if(cmd.equals("First")){
            tablePane.goToFirstPacket();
        } else if(cmd.equals("Last")){
            tablePane.goToLastPacket();
        } else if(cmd.equals("GoToPacket")){
            String input = JOptionPane.showInputDialog(this,
                    resourceManager.getString("input"),
                    resourceManager.getString("goToPacket.text"), JOptionPane.QUESTION_MESSAGE);
            try {
                int go = Integer.valueOf(input);
                tablePane.goToPacket(go);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, resourceManager.getString("formatError"),
                        resourceManager.getString("error"), JOptionPane.ERROR_MESSAGE);
            }
        } else if(cmd.equals("Open")){
            this.captor.LoadPacketsFromFile();
        } else if(cmd.equals("Save")){
            this.captor.SavePacketsToFile();
        } else if(cmd.equals("Close")){
            this.captor.clear();
            this.setCloseEnabled(false);
            this.setReloadEnabled(false);
        } else if(cmd.equals("Reload")){
            this.captor.Reload();
        } else if(cmd.equals("Quit")){
            int ret = captor.saveIfNot();
            if(ret != JOptionPane.CANCEL_OPTION){
                setVisible(false);
                dispose();
                saveProperty();
                captor.clear();
                Program.isCaptureFrameActived = false;
            }
        } else if(cmd.equals("Configuration")){
            ConfigDialog dialog = new ConfigDialog(this, resourceManager.getString("configuration.text"), true);
            dialog.setVisible(true);
        } else if(cmd.equals("ColorRules")){
            ColorRulesDialog colorDal = new ColorRulesDialog(this);
            colorDal.setVisible(true);
        }
    }

    public void valueChanged(ListSelectionEvent e) {
        if(e.getValueIsAdjusting()){
            return;
        }
        try {
            int index = tablePane.convertRowIndexToModel(tablePane.getSelectedRow());
            treePane.AnalyzePacket((Packet)captor.getPackets().elementAt(index));
            hexPane.showPacket((Packet)captor.getPackets().elementAt(index));
            tablePane.setFilterLabel();
        } catch (Exception ex) {
        }
    }


    protected void createMenuBar(){
        menuBar = new JMenuBar();
        fileMenu=new JMenu(resourceManager.getString("file.text"));
        fileMenu.setMnemonic('F');
        goMenu=new JMenu(resourceManager.getString("go.text"));
        goMenu.setMnemonic('G');
        viewMenu=new JMenu(resourceManager.getString("view.text"));
        viewMenu.setMnemonic('V');
        captureMenu=new JMenu(resourceManager.getString("capture.text"));
        captureMenu.setMnemonic('C');
        statsMenu=new JMenu(resourceManager.getString("statistics.text"));
        statsMenu.setMnemonic('S');
        bandwidthMenu=new JMenu(resourceManager.getString("bandwidth.text"));
        bandwidthMenu.setMnemonic('B');
        //analysisMenu=new JMenu(resourceManager.getString("analysis.text"));
        //analysisMenu.setMnemonic('A');
        helpMenu= new JMenu(resourceManager.getString("help.text"));
        helpMenu.setMnemonic('H');
        
        createFileMenu();
        createViewMenu();
        createCaptureMenu();
        createStatisticsMenu();
        createGoMenu();
        //createBandwidthMenu();
        //createAnalysisMenu();
        createHelpMenu();

        menuBar.add(fileMenu);
        menuBar.add(viewMenu);
        menuBar.add(captureMenu);
        menuBar.add(goMenu);
        menuBar.add(statsMenu);
        //menuBar.add(bandwidthMenu);
        //menuBar.add(analysisMenu);
        menuBar.add(helpMenu);

        createMenuLogo();
    }

    private void createFileMenu(){
        openMenu=new JMenuItem(resourceManager.getString("open.text"));
        openMenu.setMnemonic('O');
        openMenu.setIcon(resourceManager.createImageIcon("fileopen16.png", "Open"));
        openMenu.setActionCommand("Open");
        openMenu.addActionListener(this);
        openMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
        closeMenu=new JMenuItem(resourceManager.getString("close.text"));
        closeMenu.setMnemonic('C');
        closeMenu.setActionCommand("Close");
        closeMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK));
        closeMenu.addActionListener(this);
        saveMenu=new JMenuItem(resourceManager.getString("save.text"));
        saveMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
        saveMenu.setMnemonic('V');
        saveMenu.setIcon(resourceManager.createImageIcon("filesave16.png", "Save"));
        saveMenu.setActionCommand("Save");
        saveMenu.addActionListener(this);
        saveAsMenu=new JMenuItem(resourceManager.getString("saveAs.text"));
        saveAsMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK + InputEvent.SHIFT_MASK));
        saveAsMenu.setMnemonic('A');
        saveAsMenu.setIcon(resourceManager.createImageIcon("filesaveas16.png", "SaveAs"));
        saveAsMenu.setActionCommand("SaveAs");
        saveAsMenu.addActionListener(this);
        saveMenu.setEnabled(false);
        configurationMenu=new JMenuItem(resourceManager.getString("configuration.text"));
        configurationMenu.setMnemonic('C');
        configurationMenu.setActionCommand("Configuration");
        configurationMenu.setIcon(resourceManager.createImageIcon("configure16.png", "Configuration"));
        configurationMenu.addActionListener(this);
        quitMenu=new JMenuItem(resourceManager.getString("quit.text"));
        quitMenu.setActionCommand("Quit");
        quitMenu.setIcon(resourceManager.createImageIcon("quit16.png", "Configuration"));
        quitMenu.addActionListener(this);
        quitMenu.setMnemonic('Q');
        fileMenu.add(openMenu);
        fileMenu.add(closeMenu);
        fileMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        fileMenu.add(saveMenu);
        fileMenu.add(saveAsMenu);
        fileMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        fileMenu.add(configurationMenu);
        fileMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        fileMenu.add(quitMenu);
    }

    private void createViewMenu(){
        mainToolbarMenu=new JCheckBoxMenuItem(resourceManager.getString("mainToolbar.text"), true);
        mainToolbarMenu.setActionCommand("MainToolbarVisible");
        mainToolbarMenu.addItemListener(this);
        filterToolbarMenu=new JCheckBoxMenuItem(resourceManager.getString("filterToolbar.text"), true);
        filterToolbarMenu.setActionCommand("FilterToolbarVisible");
        filterToolbarMenu.addItemListener(this);
        statusBarMenu=new JCheckBoxMenuItem(resourceManager.getString("statusBar.text"), true);
        statusBarMenu.setActionCommand("StatusbarVisible");
        statusBarMenu.addItemListener(this);
        lookAndFeelMenu = new JMenu(resourceManager.getString("lookAndFeel.text"));
        packetsListMenu=new JCheckBoxMenuItem(resourceManager.getString("packetsList.text"), true);
        packetsListMenu.setActionCommand("PacketsList");
        packetsListMenu.addItemListener(this);
        packetDetailsMenu=new JCheckBoxMenuItem(resourceManager.getString("packetDetails.text"), true);
        packetDetailsMenu.setActionCommand("PacketDetail");
        packetDetailsMenu.addItemListener(this);
        packetBytesMenu=new JCheckBoxMenuItem(resourceManager.getString("packetBytes.text"), true);
        packetBytesMenu.setActionCommand("PacketBytes");
        packetBytesMenu.addItemListener(this);
        captureFilterMenu= new JCheckBoxMenuItem(resourceManager.getString("captureFilter.text"), true);
        captureFilterMenu.setActionCommand("CaptureFilter");
        captureFilterMenu.addItemListener(this);
        captureFilterMenu.setMnemonic('C');
        colorRulesMenu=new JMenuItem(resourceManager.getString("colorRules.text"));
        colorRulesMenu.setActionCommand("ColorRules");
        colorRulesMenu.addActionListener(this);
        createCoreLookAndFeelMenu();
        createSubstanceLookAndFeelMenu();
        lookAndFeelMenu.add(coreLookandfeelMenu);
        lookAndFeelMenu.add(substanceLookAndFeelMenu);
        lookAndFeelMenu.setIcon(resourceManager.createImageIcon("appearance16.png", "Look and feel"));
        viewMenu.add(lookAndFeelMenu);
        createTableViewMenu();
        viewMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        viewMenu.add(mainToolbarMenu);
        viewMenu.add(filterToolbarMenu);
        viewMenu.add(statusBarMenu);
        viewMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        viewMenu.add(packetsListMenu);
        viewMenu.add(packetDetailsMenu);
        viewMenu.add(packetBytesMenu);
        viewMenu.add(captureFilterMenu);
        viewMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        viewMenu.add(colorRulesMenu);
    }

    private void createTableViewMenu(){
        
        tableViewMenu[0]=new JMenu(resourceManager.getString("datalinkLayer.text"));
        tableViewMenu[1]=new JMenu(resourceManager.getString("networkLayer.text"));
        tableViewMenu[2]=new JMenu(resourceManager.getString("transportLayer.text"));
        tableViewMenu[3]=new JMenu(resourceManager.getString("applicationLayer.text"));
        popupTableViewMenu[0]=new JMenu(resourceManager.getString("datalinkLayer.text"));
        popupTableViewMenu[1]=new JMenu(resourceManager.getString("networkLayer.text"));
        popupTableViewMenu[2]=new JMenu(resourceManager.getString("transportLayer.text"));
        popupTableViewMenu[3]=new JMenu(resourceManager.getString("applicationLayer.text"));
        subItemTableViewReference = new ArrayList();
        subItemPopupTableViewReference = new ArrayList();
        analyzers = JNPacketAnalyzerLoader.getAnalyzers();
        for(int i=0; i< analyzers.length;i++){
            itemTableView = new JMenu(analyzers[i].getProtocolName());
            itemPopupTableView = new JMenu(analyzers[i].getProtocolName());
            String[] valueNames = analyzers[i].getValueNames();
            if(valueNames==null) continue;
            for(int j=0;j<valueNames.length;j++){
                subItemTableView = new JCheckBox(valueNames[j]);
                subItemPopupTableView = new JCheckBox(valueNames[j]);
                subItemTableView.setActionCommand("TableView" + i + " " + j + " ");
                subItemPopupTableView.setActionCommand("TableView" + i + " " + j + " ");
                subItemTableViewReference.add(subItemTableView);
                subItemPopupTableViewReference.add(subItemPopupTableView);
                subItemTableView.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        String cmd = e.getActionCommand();
                        if(cmd.startsWith("TableView")){

                            int index=Integer.parseInt(cmd.substring(9,11).trim());
                            int valueIndex = Integer.parseInt(cmd.substring(11, 13).trim());
                            JCheckBox item=(JCheckBox)e.getSource();
                            JNPacketAnalyzer[] tempAnalyzers = JNPacketAnalyzerLoader.getAnalyzers();
                            tablePane.setJNTableView(tempAnalyzers[index],valueIndex,item.isSelected());
                            int temp = subItemTableViewReference.indexOf(item);
                            ((JCheckBox)subItemPopupTableViewReference.get(temp)).setSelected(item.isSelected());
                        }
                    }
                });
                subItemPopupTableView.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        String cmd = e.getActionCommand();
                        if(cmd.startsWith("TableView")){
                            int index=Integer.parseInt(cmd.substring(9,11).trim());
                            int valueIndex = Integer.parseInt(cmd.substring(11, 13).trim());
                            JCheckBox item=(JCheckBox)e.getSource();
                            JNPacketAnalyzer[] tempAnalyzers = JNPacketAnalyzerLoader.getAnalyzers();
                            tablePane.setJNTableView(tempAnalyzers[index],valueIndex,item.isSelected());
                            int temp = subItemPopupTableViewReference.indexOf(item);
                            ((JCheckBox)subItemTableViewReference.get(temp)).setSelected(item.isSelected());
                        }
                    }
                });
                itemTableView.add(subItemTableView);
                itemPopupTableView.add(subItemPopupTableView);
            }
            tableViewMenu[analyzers[i].layer].add(itemTableView);
            popupTableViewMenu[analyzers[i].layer].add(itemPopupTableView);
        }
        JMenu tbViewMenu = new JMenu(resourceManager.getString("tableView.text"));
        popup = new JPopupMenu();
        for(int i=0; i< tableViewMenu.length; i++){
            tbViewMenu.add(tableViewMenu[i]);
            popup.add(popupTableViewMenu[i]);
        }
        viewMenu.add(tbViewMenu);
        loadProperty();
    }

    private void createCaptureMenu(){
        startMenu= new JMenuItem(resourceManager.getString("start.text"));
        startMenu.setActionCommand("Start");
        startMenu.addActionListener(this);
        startMenu.setMnemonic('S');
        startMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
        startMenu.setIcon(resourceManager.createImageIcon("player_play16.png", "Start"));
        stopMenu= new JMenuItem(resourceManager.getString("stop.text"));
        stopMenu.setActionCommand("Stop");
        stopMenu.addActionListener(this);
        stopMenu.setMnemonic('t');
        stopMenu.setIcon(resourceManager.createImageIcon("player_pause16.png", "Stop"));
        stopMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
        restartMenu= new JMenuItem(resourceManager.getString("restart.text"));
        restartMenu.setActionCommand("Restart");
        restartMenu.addActionListener(this);
        restartMenu.setMnemonic('R');
        restartMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
        restartMenu.setIcon(resourceManager.createImageIcon("Restart-16.png", "Restart"));
        
        captureMenu.add(startMenu);
        captureMenu.add(stopMenu);
        captureMenu.add(restartMenu);
    }

    private void createStatisticsMenu(){
        cumulativeMenu= new JMenu(resourceManager.getString("cumulative.text"));
        cumulativeMenu.setIcon(resourceManager.createImageIcon("Pie-chart24.png", "Cumulative"));
        continuousMenu= new JMenu(resourceManager.getString("continuous.text"));
        continuousMenu.setIcon(resourceManager.createImageIcon("Graph24.png", "Continuous"));
        JMenuItem item;
        JNStatisticsController[] controllers = JNStatisticsControllerLoader.getControllers();
        for(int i=0; i<controllers.length; i++){
            item = new JMenuItem(controllers[i].getName());
            item.setActionCommand("Cumstat" + i);
            item.addActionListener(statisticListener);
            cumulativeMenu.add(item);
        }
        for(int i=0; i<controllers.length; i++){
            item = new JMenuItem(controllers[i].getName());
            item.setActionCommand("Constat" + i);
            item.addActionListener(statisticListener);
            continuousMenu.add(item);
        }
        statsMenu.add(cumulativeMenu);
        statsMenu.add(continuousMenu);
    }

    private void createGoMenu(){
        backMenu=new JMenuItem(resourceManager.getString("back.text"));
        backMenu.setActionCommand("Back");
        backMenu.addActionListener(this);
        backMenu.setMnemonic('B');
        backMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.ALT_MASK));
        backMenu.setIcon(resourceManager.createImageIcon("previous16.png", "Back"));
        forwardMenu=new JMenuItem(resourceManager.getString("forward.text"));
        forwardMenu.setActionCommand("Forward");
        forwardMenu.addActionListener(this);
        forwardMenu.setMnemonic('F');
        forwardMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.ALT_MASK));
        forwardMenu.setIcon(resourceManager.createImageIcon("next16.png", "Forward"));
        previousMenu= new JMenuItem(resourceManager.getString("previous.text"));
        previousMenu.setActionCommand("Previous");
        previousMenu.addActionListener(this);
        previousMenu.setMnemonic('P');
        previousMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK));
        previousMenu.setIcon(resourceManager.createImageIcon("up16.png", "Previous"));
        nextMenu= new JMenuItem(resourceManager.getString("next.text"));
        nextMenu.setActionCommand("Next");
        nextMenu.addActionListener(this);
        nextMenu.setMnemonic('N');
        nextMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK));
        nextMenu.setIcon(resourceManager.createImageIcon("down16.png", "Next"));
        firstMenu= new JMenuItem(resourceManager.getString("first.text"));
        firstMenu.setActionCommand("First");
        firstMenu.addActionListener(this);
        firstMenu.setMnemonic('i');
        firstMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, InputEvent.CTRL_MASK));
        lastMenu= new JMenuItem(resourceManager.getString("last.text"));
        lastMenu.setActionCommand("Last");
        lastMenu.addActionListener(this);
        lastMenu.setMnemonic('L');
        lastMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_END, InputEvent.CTRL_MASK));
        goToPacketMenu= new JMenuItem(resourceManager.getString("goToPacket.text"));
        goToPacketMenu.setActionCommand("GoToPacket");
        goToPacketMenu.addActionListener(this);
        goToPacketMenu.setMnemonic('G');
        goToPacketMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK));
        goToPacketMenu.setIcon(resourceManager.createImageIcon("goTo.png", "Go to packet"));
        goMenu.add(backMenu);
        goMenu.add(forwardMenu);
        goMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        goMenu.add(previousMenu);
        goMenu.add(nextMenu);
        goMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        goMenu.add(firstMenu);
        goMenu.add(lastMenu);
        goMenu.add(goToPacketMenu);
    }

    private void createBandwidthMenu(){
        managementMenu= new JMenuItem(resourceManager.getString("management.text"));
        reportMenu= new JMenuItem(resourceManager.getString("report.text"));
        blockURLMenu= new JMenuItem(resourceManager.getString("blockURL.text"));
        blockIPMenu= new JMenuItem(resourceManager.getString("blockIP.text"));
        blockPortMenu= new JMenuItem(resourceManager.getString("blockPort.text"));
        bandwidthMenu.add(managementMenu);
        bandwidthMenu.add(reportMenu);
        bandwidthMenu.add(new JSeparator(JSeparator.HORIZONTAL));
        bandwidthMenu.add(blockURLMenu);
        bandwidthMenu.add(blockIPMenu);
        bandwidthMenu.add(blockPortMenu);
    }

    private void createAnalysisMenu(){
        networkVisualizingMenu= new JMenuItem(resourceManager.getString("networkVisualizing.text"));
        packetsVisualizingMenu= new JMenuItem(resourceManager.getString("packetsVisualizing.text"));
        analysisMenu.add(networkVisualizingMenu);
        analysisMenu.add(packetsVisualizingMenu);
    }

    private void createHelpMenu(){
        helpContentsMenu= new JMenuItem(resourceManager.getString("helpContents.text"));
        helpContentsMenu.setMnemonic(KeyEvent.VK_F1);
        helpContentsMenu.setIcon(resourceManager.createImageIcon("help24.png", "Help"));
        aboutMenu= new JMenuItem(resourceManager.getString("about.text"));
        helpMenu.add(helpContentsMenu);
        helpMenu.add(aboutMenu);
    }
    private void createToolbar(){
        JToolBar tempToobar = new JToolBar();
        cumulativeStatPopup = new JPopupMenu("aaa");
        continousStatPopup = new JPopupMenu("bbb");
        JMenuItem item;
        JNStatisticsController[] controllers = JNStatisticsControllerLoader.getControllers();
        for(int i=0; i<controllers.length; i++){
            item = new JMenuItem(controllers[i].getName());
            item.setActionCommand("Cumstat" + i);
            item.addActionListener(statisticListener);
            cumulativeStatPopup.add(item);
        }
        for(int i=0; i<controllers.length; i++){
            item = new JMenuItem(controllers[i].getName());
            item.setActionCommand("Constat" + i);
            item.addActionListener(statisticListener);
            continousStatPopup.add(item);
        }
        openButton = new JButton(resourceManager.createImageIcon("fileopen24.png", "Open"));
        openButton.setActionCommand("Open");
        openButton.addActionListener(this);
        saveButton = new JButton(resourceManager.createImageIcon("filesave24.png", "Save"));
        saveButton.setActionCommand("Save");
        saveButton.addActionListener(this);
        saveAsButton = new JButton(resourceManager.createImageIcon("filesaveas24.png", "Save As"));
        saveAsButton.setActionCommand("SaveAs");
        saveAsButton.addActionListener(this);
        closeButton = new JButton(resourceManager.createImageIcon("fileclose24.png", "Close"));
        closeButton.setActionCommand("Close");
        closeButton.addActionListener(this);
        reloadButton = new JButton(resourceManager.createImageIcon("reload24.png", "Reload"));
        reloadButton.setActionCommand("Reload");
        reloadButton.addActionListener(this);
        startButton = new JButton(resourceManager.createImageIcon("player_play24.png", "Start"));
        startButton.setActionCommand("Start");
        startButton.addActionListener(this);
        stopButton = new JButton(resourceManager.createImageIcon("player_pause24.png", "Stop"));
        stopButton.setActionCommand("Stop");
        stopButton.addActionListener(this);
        restartButton = new JButton(resourceManager.createImageIcon("Restart-24.png", "Restart"));
        restartButton.setActionCommand("Restart");
        restartButton.addActionListener(this);
        backButton = new JButton(resourceManager.createImageIcon("previous24.png", "Back"));
        backButton.setActionCommand("Back");
        backButton.addActionListener(this);
        forwardButton = new JButton(resourceManager.createImageIcon("next24.png", "Forward"));
        forwardButton.setActionCommand("Forward");
        forwardButton.addActionListener(this);
        goToPacketButton = new JButton(resourceManager.createImageIcon("goToPacket24.png", "GoToPacket"));
        goToPacketButton.setActionCommand("GoToPacket");
        goToPacketButton.addActionListener(this);
        previousButton = new JButton(resourceManager.createImageIcon("up24.png", "Previous"));
        previousButton.setActionCommand("Previous");
        previousButton.addActionListener(this);
        nextButton = new JButton(resourceManager.createImageIcon("down24.png", "Next"));
        nextButton.setActionCommand("Next");
        nextButton.addActionListener(this);
        cumulativeButton = new JButton(resourceManager.createImageIcon("Pie-chart24.png", "Cumulative"));
        cumulativeButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                cumulativeStatPopup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
        continuousButton = new JButton(resourceManager.createImageIcon("Graph24.png", "Continuous"));
        continuousButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                continousStatPopup.show(e.getComponent(), e.getX(), e.getY());
            }
            
        });
        quitButton = new JButton(resourceManager.createImageIcon("cancel24.png", "Quit"));
        quitButton.setAlignmentX(RIGHT_ALIGNMENT);
        quitButton.setActionCommand("Quit");
        quitButton.addActionListener(this);
        helpContentsButton = new JButton(resourceManager.createImageIcon("help24.png", "Help"));
        helpContentsButton.setActionCommand("Help");
        helpContentsButton.addActionListener(this);
        colorizePacketsListButton = new JToggleButton(resourceManager.createImageIcon(
                "Gnome-Colors-Emblem-Green-2.png", "Colorize Packets List"), false);
        autoSrollPacketsListButton = new JToggleButton(resourceManager.createImageIcon(
                "Scroll-down-24.png","Auto Scroll"), true);
        reconstructTcpButton = new JButton(resourceManager.createImageIcon("connect_established-32.png", "Reconstruct"));





        tempToobar.setLayout(new BoxLayout(tempToobar, BoxLayout.X_AXIS));
        tempToobar.add(openButton);
        tempToobar.add(saveButton);
        tempToobar.add(saveAsButton);
        tempToobar.add(closeButton);
        tempToobar.add(reloadButton);
        tempToobar.addSeparator();
        tempToobar.add(startButton);
        tempToobar.add(stopButton);
        tempToobar.add(restartButton);
        tempToobar.addSeparator();
        tempToobar.add(backButton);
        tempToobar.add(forwardButton);
        tempToobar.add(goToPacketButton);
        tempToobar.add(previousButton);
        tempToobar.add(nextButton);
        tempToobar.addSeparator();
        tempToobar.add(cumulativeButton);
        tempToobar.add(continuousButton);
        tempToobar.addSeparator();
        tempToobar.add(reconstructTcpButton);
        tempToobar.add(colorizePacketsListButton);
        tempToobar.add(autoSrollPacketsListButton);
        tempToobar.add(Box.createGlue());
        tempToobar.add(helpContentsButton);
        tempToobar.add(quitButton);
        tempToobar.setFloatable(false);
        tempToobar.setBorder(new EmptyBorder(3, 3, 3, 3));
        this.toolbar = tempToobar;
    }


    private void createMenuLogo(){
        JLabel co1=JNetAnalyzerLogo.getOutLinedLogo("JNA",1,22f, Font.ITALIC,Color.darkGray);
        co1.setHorizontalAlignment(JLabel.RIGHT);
        JLabel co2=JNetAnalyzerLogo.getOutLinedLogo("nalyzer",1,14f, Font.BOLD, Color.gray);
        menuBar.add(Box.createGlue());
        menuBar.add(co1);
        menuBar.add(co2);
    }
    private void createStatusBar(){
        statusLabel = new JLabel();
        statusBar = new JXStatusBar();
        statusBar.add(statusLabel);
    }

    protected void createCoreLookAndFeelMenu(){
        coreLookandfeelMenu = new JMenu(resourceManager.getString("coreLookAndFeel.text"));
        UIManager.LookAndFeelInfo lookAndFeelInfos[] = UIManager.getInstalledLookAndFeels();
        lookAndFeel = UIManager.getLookAndFeel().getClass().getName();
        lookAndFeelRadioGroup = new ButtonGroup();
        for(UIManager.LookAndFeelInfo lafInfo: lookAndFeelInfos) {
            coreLookandfeelMenu.add(createCoreLookAndFeelItem(lafInfo.getName(), lafInfo.getClassName()));
        }
    }
    protected void createSubstanceLookAndFeelMenu(){
        substanceLookAndFeelMenu = new JMenu(resourceManager.getString("substanceLookAndFeel.text"));
        for (SkinInfo skinInfo : SubstanceLookAndFeel.getAllSkins().values()) {
            substanceLookAndFeelMenu.add(createSubstanceLookAndFeelItem(skinInfo.getDisplayName(), skinInfo.getClassName()));
        }
    }

    protected JRadioButtonMenuItem createCoreLookAndFeelItem(String lafName, String lafClassName){
        JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem();
        lafItem.setSelected(lafClassName.equals(lookAndFeel));
        lafItem.setHideActionText(true);
        lafItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ButtonModel model = lookAndFeelRadioGroup.getSelection();
                String lookAndFeelName = model.getActionCommand();
                    setCoreLookAndFeel(lookAndFeelName);
            }
        });
        lafItem.setText(lafName);
        lafItem.setActionCommand(lafClassName);
        lookAndFeelRadioGroup.add(lafItem);

        return lafItem;
    }
    protected JRadioButtonMenuItem createSubstanceLookAndFeelItem(String lafName, String lafClassName){
        JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem();
        lafItem.setSelected(lafClassName.equals(lookAndFeel));
        lafItem.setHideActionText(true);
        lafItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ButtonModel model = lookAndFeelRadioGroup.getSelection();
                String lookAndFeelName = model.getActionCommand();
                    setSubstanceLookAndFeel(lookAndFeelName);
            }
        });
        lafItem.setText(lafName);
        lafItem.setActionCommand(lafClassName);
        lookAndFeelRadioGroup.add(lafItem);

        return lafItem;
    }


    protected void setCoreLookAndFeel(String lookAndFeel) {
        String oldLookAndFeel = this.lookAndFeel;
        if(!oldLookAndFeel.equals(lookAndFeel)){
            try {
                UIManager.setLookAndFeel(lookAndFeel);
            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(this,
                            resourceManager.getString("error.unableToChangelookAndFeel" ),
                            resourceManager.getString("error.title"),
                            JOptionPane.ERROR_MESSAGE);
            } catch (InstantiationException ex) {
                JOptionPane.showMessageDialog(this,
                            resourceManager.getString("error.unableToChangelookAndFeel" ),
                            resourceManager.getString("error.title"),
                            JOptionPane.ERROR_MESSAGE);
            } catch (IllegalAccessException ex) {
                JOptionPane.showMessageDialog(this,
                            resourceManager.getString("error.unableToChangelookAndFeel" ),
                            resourceManager.getString("error.title"),
                            JOptionPane.ERROR_MESSAGE);
            } catch (UnsupportedLookAndFeelException ex) {
                JOptionPane.showMessageDialog(this,
                            resourceManager.getString("error.unableToChangelookAndFeel" ),
                            resourceManager.getString("error.title"),
                            JOptionPane.ERROR_MESSAGE);
            }
            this.lookAndFeel = lookAndFeel;
            JNMainFrame.updateLookAndFeel();
            firePropertyChange("lookAndFeel", oldLookAndFeel, lookAndFeel);
        }
    }

    protected void setSubstanceLookAndFeel(final String lookAndFeel){
        String oldLookAndFeel = this.lookAndFeel;
        if(!oldLookAndFeel.equals(lookAndFeel)){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    SubstanceLookAndFeel.setSkin(lookAndFeel);
                }
            });
        }
        this.lookAndFeel = lookAndFeel;
        JNMainFrame.updateLookAndFeel();
        firePropertyChange("lookAndFeel", oldLookAndFeel, lookAndFeel);
    }

    public static void updateLookAndFeel() {
        Window windows[] = Frame.getWindows();
        for(Window window: windows){
            SwingUtilities.updateComponentTreeUI(window);
        }
        Program.updateAllComponentLookAndFeel();
    }

    public String getLookAndFeel(){
        return lookAndFeel;
    }


    void loadProperty(){
        if(Program.JNProperty.getProperty("TableView")!=null){
            //get all menus
            Component[] menus=new Component[analyzers.length];
            Component[] popups = new Component[analyzers.length];
            int k=0;
            for(int j=0;j<tableViewMenu[0].getMenuComponents().length;j++){
                menus[k]=tableViewMenu[0].getMenuComponents()[j];
                popups[k]=popupTableViewMenu[0].getMenuComponents()[j];
                k++;
            }
                    
            for(int j=0;j<tableViewMenu[1].getMenuComponents().length;j++){
                menus[k]=tableViewMenu[1].getMenuComponents()[j];
                popups[k]=popupTableViewMenu[1].getMenuComponents()[j];
                k++;
            }
            for(int j=0;j<tableViewMenu[2].getMenuComponents().length;j++){
                menus[k]=tableViewMenu[2].getMenuComponents()[j];
                popups[k]=popupTableViewMenu[2].getMenuComponents()[j];
                k++;
            }
            for(int j=0;j<tableViewMenu[3].getMenuComponents().length;j++){
                menus[k]=tableViewMenu[3].getMenuComponents()[j];
                popups[k]=popupTableViewMenu[3].getMenuComponents()[j];
                k++;
            }


            //load ptoperty
            StringTokenizer status=new StringTokenizer(Program.JNProperty.getProperty("TableView"),",");

            while(status.hasMoreTokens()){
                StringTokenizer s=new StringTokenizer(status.nextToken(),"*");
                if(s.countTokens()==2){
                    String name=s.nextToken();
                    int valueIndex=Integer.parseInt(s.nextToken());
                    for(int i=0;i<menus.length;i++){
                        if(((JMenu)menus[i]).getText()==null || name==null) continue;
                        if(((JMenu)menus[i]).getText().equals(name)){
                            Component[] vn=((JMenu)menus[i]).getMenuComponents();
                            Component[] vn2=((JMenu)popups[i]).getMenuComponents();
                            for(int j=0;j<vn.length;j++)
                                if(valueIndex == j){
                                    ((JCheckBox)vn[j]).setSelected(true);
                                    ((JCheckBox)vn2[j]).setSelected(true);
                                    break;
                                }
                            break;
                        }
                    }
                    for(int i=0;i<analyzers.length;i++)
                        if(analyzers[i].getProtocolName().equals(name)){
                                tablePane.setJNTableView(analyzers[i],valueIndex,true);
                                break;
                        }
                }
            }
        }
    }

    void saveProperty(){
        Program.JNProperty.put("win_width", String.valueOf(this.getBounds().width));
        Program.JNProperty.put("win_height", String.valueOf(this.getBounds().height));
        Program.JNProperty.put("win_X", String.valueOf(this.getBounds().x));
        Program.JNProperty.put("win_Y", String.valueOf(this.getBounds().y));
        Program.JNProperty.setProperty("lastFile", "");
        String[] viewStatus = tablePane.getJNTableViewStatus();
        if(viewStatus.length>0){
            StringBuffer buf = new StringBuffer(viewStatus[0]);
            for(int i= 1; i< viewStatus.length; i++){
                buf.append("," + viewStatus[i]);
            }
            Program.JNProperty.put("TableView", buf.toString());
        } else {
            Program.JNProperty.put("TableView", "");
        }
        Program.saveProperty();
    }

    javax.swing.Timer JNMainFrameUpdate = new javax.swing.Timer(500, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            tablePane.fireTableChanged();
            UpdateStatusBar();
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    if(autoScroll){
                        tablePane.scrollRectToVisible();
                    }
                }
            });
        }
    });
    public void UpdateStatusBar(){
        statusLabel.setText("Captured " + captor.packets.size() + " packets: " + tablePane.getRowCount() + " visible");
    }

    public void startUpdating(){
        JNMainFrameUpdate.setRepeats(true);
        JNMainFrameUpdate.start();
    }

    public void stopUpdating(){
        JNMainFrameUpdate.stop();
        JNMainFrameUpdate.setRepeats(false);
        JNMainFrameUpdate.start();
    }


    public void createMainPane(int style){
        switch(style){
            case 1: {
                splitPane1 = new AnimatingSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                splitPane2 = new AnimatingSplitPane(JSplitPane.VERTICAL_SPLIT);
                splitPane2.setTopComponent(tablePane);
                JPanel nowrapHexPanel = new JPanel(new BorderLayout());
                nowrapHexPanel.add(hexPane);
                JScrollPane scrollHexPane = new JScrollPane(nowrapHexPanel);
                splitPane2.setBottomComponent(scrollHexPane);
                splitPane2.setContinuousLayout(true);
                splitPane1.setTopComponent(treePane);
                splitPane1.setBottomComponent(splitPane2);
                splitPane1.setOneTouchExpandable(true);
                splitPane1.setContinuousLayout(true);
                splitPane1.setDividerLocation(300);
                this.getContentPane().add(splitPane1, BorderLayout.CENTER);
                break;
            }
            case 2:{
                splitPane1 = new AnimatingSplitPane(JSplitPane.VERTICAL_SPLIT);
                splitPane2 = new AnimatingSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                splitPane2.setTopComponent(treePane);
                JPanel nowrapHexPanel = new JPanel(new BorderLayout());
                nowrapHexPanel.add(hexPane);
                JScrollPane scrollHexPane = new JScrollPane(nowrapHexPanel);
                splitPane2.setBottomComponent(scrollHexPane);
                splitPane2.setContinuousLayout(true);
                splitPane2.setDividerLocation(600);
                splitPane1.setTopComponent(tablePane);
                splitPane1.setBottomComponent(splitPane2);
                splitPane1.setContinuousLayout(true);
                splitPane1.setDividerLocation(400);
                this.getContentPane().add(splitPane1, BorderLayout.CENTER);
                this.getContentPane().add(statusBar, BorderLayout.PAGE_END);
                break;
            }
            case 3:{
                splitPane1 = new AnimatingSplitPane(JSplitPane.VERTICAL_SPLIT);
                splitPane2 = new AnimatingSplitPane(JSplitPane.VERTICAL_SPLIT);
                splitPane2.setTopComponent(treePane);
                JPanel nowrapHexPanel = new JPanel(new BorderLayout());
                nowrapHexPanel.add(hexPane);
                JScrollPane scrollHexPane = new JScrollPane(nowrapHexPanel);
                splitPane2.setBottomComponent(scrollHexPane);
                splitPane2.setContinuousLayout(true);
                splitPane2.setDividerLocation( 500);
                splitPane1.setTopComponent(tablePane);
                splitPane1.setBottomComponent(splitPane2);
                splitPane1.setContinuousLayout(true);
                splitPane1.setDividerLocation(300);
                this.getContentPane().add(splitPane1, BorderLayout.CENTER);
                break;
            }
        }
    }

    public void setStartEnabled(boolean flag){
        this.startMenu.setEnabled(flag);
        this.startButton.setEnabled(flag);
    }

    public void setStopEnabled(boolean flag){
        this.stopMenu.setEnabled(flag);
        this.stopButton.setEnabled(flag);
    }
    public void setRestartEnabled(boolean flag){
        this.restartMenu.setEnabled(flag);
        this.restartButton.setEnabled(flag);
    }
    public void setReloadEnabled(boolean flag){
        this.reloadButton.setEnabled(flag);
    }
    public void setCloseEnabled(boolean flag){
        this.closeButton.setEnabled(flag);
        this.closeMenu.setEnabled(flag);
    }
    public void setOpenEnabled(boolean flag){
        this.openButton.setEnabled(flag);
        this.openMenu.setEnabled(flag);
    }
    public void setSaveEnabled(boolean flag){
        this.saveButton.setEnabled(flag);
        this.saveMenu.setEnabled(flag);
    }
    public void setSaveAsEnabled(boolean flag){
        this.saveAsButton.setEnabled(flag);
        this.saveAsMenu.setEnabled(flag);
    }
    public void setNavigatorEnabled(boolean flag){
        this.goToPacketButton.setEnabled(flag);
        this.nextButton.setEnabled(flag);
        this.previousButton.setEnabled(flag);
        this.nextMenu.setEnabled(flag);
        this.previousMenu.setEnabled(flag);
        this.goToPacketMenu.setEnabled(flag);
    }
    public void setUndoRedoEnabled(boolean flag){
        this.backButton.setEnabled(flag);
        this.backMenu.setEnabled(flag);
        this.forwardButton.setEnabled(flag);
        this.forwardMenu.setEnabled(flag);
    }

    private void installOtherButtonHandler(){
        autoSrollPacketsListButton.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                int state = e.getStateChange();
                if(state == ItemEvent.SELECTED){
                    autoScroll = true;
                } else {
                    autoScroll = false;
                }
            }
        });
        colorizePacketsListButton.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                int state = e.getStateChange();
                if(state == ItemEvent.SELECTED){
                    tablePane.setIsApplyColorRules(true);
                } else {
                    tablePane.setIsApplyColorRules(false);
                }
            }
        });
        reconstructTcpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                int ret = chooser.showSaveDialog(getInstance());
                if(ret == JFileChooser.APPROVE_OPTION){
                    final File file = JNMainFrame.chooser.getSelectedFile();
                    Runnable r = new Runnable() {
                        public void run() {
                            try {
                            FileOutputStream fosRequest, fosResponse;
                            File fRequest, fResponse;
                            FileDataReconstructor fileDataReconstructor = new FileDataReconstructor();
                            Map<TcpConnection, TcpReassembler> map = fileDataReconstructor.reconstruct(file, new JpcapReconstructor(new PacketReassembler()));
                            Http http = new Http(map);
                            Map<TcpConnection, HttpOutput> httpPackets = http.packetize();
                            System.out.println("number of packets " + httpPackets.size());
                            for (TcpConnection tcpConnection : httpPackets.keySet()) {
                                System.out.println(tcpConnection);
                                HttpOutput httpOutput = httpPackets.get(tcpConnection);
                                String nameRequest = file.getAbsolutePath() + "_" + tcpConnection.toString() +"_request" + ".data";
                                String nameResponse = file.getAbsolutePath() + "_" + tcpConnection.toString() +"response" + ".data";
                                byte[] httpOutputRequest = httpOutput.getRequest().getData();
                                byte[] httpOutputResponse = httpOutput.getResponse().getData();
                                fRequest = new File(nameRequest);
                                fosRequest = new FileOutputStream(fRequest);
                                fResponse = new File(nameResponse);
                                fosResponse = new FileOutputStream(fResponse);
                                fosRequest.write(httpOutputRequest);
                                fosResponse.write(httpOutputResponse);
                            }
                        } catch (Exception ex) {
                        }
                        }
                    };
                    new Thread(r).start();
                    
                }
            }
        });
    }
    public JNMainFrame getInstance(){
        return this;
    }

    public void updateColorRules(){
        tablePane.model.updateProtocolColors();
        tablePane.model.fireTableStructureChanged();
    }

    public void itemStateChanged(ItemEvent e) {
        int state = e.getStateChange();
        String cmd = ((JCheckBoxMenuItem)e.getSource()).getActionCommand();
        if(cmd.equals("MainToolbarVisible")){
            if(state == ItemEvent.SELECTED){
                if(this.toolbar != null){
                    toolbar.setVisible(true);
                }
            } else {
                if(this.toolbar != null){
                    toolbar.setVisible(false);
                }
            }
        } else if(cmd.equals("FilterToolbarVisible")){
            if(state == ItemEvent.SELECTED){
                if(this.tablePane != null){
                    tablePane.setFilterVisible(true);
                }
            } else {
                if(this.tablePane != null){
                    tablePane.setFilterVisible(false);
                }
            }
        } else if(cmd.equals("StatusbarVisible")){
            if(state == ItemEvent.SELECTED){
                if(statusBar != null){
                    statusBar.setVisible(true);
                }
            } else {
                if(statusBar != null){
                    statusBar.setVisible(false);
                }
            }
        } else if(cmd.equals("PacketsList")){
            if(state == ItemEvent.SELECTED){
                if(tablePane != null){
                    tablePane.setVisible(true);
                }
            } else {
                if(tablePane != null){
                    tablePane.setVisible(false);
                }
            }
        } else if(cmd.equals("PacketDetail")){
            if(state == ItemEvent.SELECTED){
                if(treePane != null){
                    treePane.setVisible(true);
                }
            } else {
                if(treePane != null){
                    treePane.setVisible(false);
                }
            }
        } else if(cmd.equals("PacketBytes")){
            if(state == ItemEvent.SELECTED){
                if(hexPane != null){
                    hexPane.setVisible(true);
                }
            } else {
                if(hexPane != null){
                    hexPane.setVisible(false);
                }
            }
        }
    }


    class StatisticListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if(cmd.startsWith("Cumstat")){
                int index = Integer.parseInt(cmd.substring(7));
                captor.addCumulativeStatDialog(JNStatisticsControllerLoader.getControllerAt(index));
            } else if(cmd.startsWith("Constat")){
                int index = Integer.parseInt(cmd.substring(7));
                captor.addContinuousStatDialog(JNStatisticsControllerLoader.getControllerAt(index));
            }
        }

    }
    


}
